<template lang="html">
  <div class="binner-container">
    <el-form ref="form" label-width="120px">
      <el-form-item label="添加展示图">
        <!-- action="https://jsonplaceholder.typicode.com/posts/" -->
        <el-upload
          class="avatar-uploader"
          :action="baseUrl+'/wuye/upload'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <i class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </el-form-item>

      <el-form-item label="展示图列表">
        <el-table v-loading="loading" :border="true" :data="list">
          <el-table-column label="展示图" align="center">
            <template slot-scope="{row}">
              <el-image
                style="width: 100px; height: 100px"
                :src="row.img"
                fit="contain"
              />
            </template>
          </el-table-column>
          <el-table-column label="操作" width="100" align="center">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(scope.$index, scope.row)"
              > 删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
import { getBinnerList, createBinner, delBinner } from '@/api/binner'
const baseUrl = process.env.VUE_APP_BASE_API
export default {
  data() {
    return {
      baseUrl,
      list: [],
      loading: false
    }
  },
  created() {
    this.fetchBinnerList()
  },
  methods: {
    async fetchBinnerList() {
      this.loading = true
      const {
        data = []
      } = await getBinnerList()
      console.log(data)
      this.list = data
      this.loading = false
    },
    async handleAvatarSuccess(res, file) {
      console.log(res, file)
      const {
        file_path
      } = res
      const result = await createBinner(file_path)
      // 在这里添加展示图
      this.list.unshift({
        img: file_path
      })
      this.$message({
        type: 'success',
        message: result.message
      })
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 或 PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    async handleDelete(index, row) {
      const result = await delBinner(row.id)
      this.list.splice(index, 1)
      this.$message({
        message: `(${row.id})${result.message}`,
        type: 'success'
      })
    }
  }
}
</script>

<style lang="scss">
.binner-container {
  padding: 50px;
  padding-left: 0;
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 80px;
    height: 80px;
    line-height: 80px;
    text-align: center;
  }
  .avatar {
    width: 80px;
    height: 80px;
    display: block;
  }
}
</style>
